package com.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.common.ajax.AjaxRes;
import com.common.mybatis.Page;
import com.common.utils.base.Const;
import com.controller.base.BaseController;
import com.entity.Column;
import com.entity.News;
import com.entity.User;
import com.service.ColumnService;
import com.service.NewsService;

@Controller
@RequestMapping("/news")
public class NewsController extends BaseController<News>{
	
	@Autowired
	private NewsService newsService;
	@Autowired
	private ColumnService columnService;

	@RequestMapping("/index")
	public String index(){
		return "pages/news/news";
	}
	
	@RequestMapping("/info")
	@ResponseBody
	public AjaxRes findByPage(News news, Page<News> pages){
		AjaxRes res = new AjaxRes();
		pages = newsService.findByPage(news, pages);
		res.setSucceed(pages, Const.DATA_SUCCEED);
		return res;
	}
	
	@RequestMapping("/detail")
	@ResponseBody
	public AjaxRes find(News news){
		AjaxRes res = new AjaxRes();
		news = newsService.find(news).get(0);
		res.setSucceed(news,Const.DATA_SUCCEED);
		return res;
	}
	
	@RequestMapping("/add")
	@ResponseBody
	public AjaxRes add(@RequestBody Map<String,Object> map){
		AjaxRes res = new AjaxRes();
		//Map 转为JSON 再反序列化为bean
		String json = JSONArray.toJSONString(map.get("obj"));
		News news = JSON.parseArray(json, News.class).get(0);
		User u = (User) getRequest().getSession().getAttribute("loggedIn");
		news.setPublisher(u.getUserName());
		//添加默认封面
		if(news.getCover() == null || news.getCover() == ""){
			news.setCover("SpringDemo/upload/file/news.png");
		}
		newsService.insert(news);
		res.setSucceedMsg(Const.SAVE_SUCCEED);
		return res;
	}

	@RequestMapping("/upd")
	@ResponseBody
	public AjaxRes update(@RequestBody Map<String,Object> map){
		AjaxRes res = new AjaxRes();
		String json = JSONArray.toJSONString(map.get("obj"));
		News news = JSON.parseArray(json, News.class).get(0);
		//添加默认封面
		if(news.getCover() == null || news.getCover() == ""){
			news.setCover("SpringDemo/upload/file/news.png");
		}
		newsService.update(news);
		res.setSucceedMsg(Const.UPDATE_SUCCEED);
		return res;
	}
	
	@RequestMapping("/del")
	@ResponseBody
	public AjaxRes delete(News news){
		AjaxRes res = new AjaxRes();
		newsService.delete(news);
		res.setSucceed(Const.DEL_SUCCEED);
		return res;
	}
	
	@RequestMapping("/delall")
	@ResponseBody
	public AjaxRes delAllnews(@RequestBody List<News> news){
		AjaxRes res = new AjaxRes();
		newsService.deleteBatch(news);
		res.setSucceedMsg(Const.DEL_SUCCEED);
		return res;
	}
	
	@RequestMapping("/del/pic")
	@ResponseBody
	public AjaxRes delPis(News news){
		AjaxRes res = new AjaxRes();
		news = newsService.find(news).get(0);
		news.setCover("");
		newsService.update(news);
		res.setSucceedMsg(Const.DEL_SUCCEED);
		return res;
	}
	
	//新闻栏目管理
	@RequestMapping("/col")
	public String col(){
		return "pages/news/column";
	}
	
	@RequestMapping("/col/all")
	@ResponseBody
	public AjaxRes findColumns(){
		AjaxRes res = new AjaxRes();
		List<Column> cols = columnService.find(null);
		res.setSucceed(cols, Const.DATA_SUCCEED);
		return res;
	}
	
	
	@RequestMapping("/col/tree/upper")
	@ResponseBody
	public AjaxRes findCols(){
		AjaxRes res = new AjaxRes();
		List<Column> fCols = columnService.findUpper(null);
		res.setSucceed(fCols, Const.DATA_SUCCEED);
		return res;
	}
	
	@RequestMapping("/col/tree/lower")
	@ResponseBody
	public AjaxRes findCols(Column col){
		AjaxRes res = new AjaxRes();
		List<Column> sCols = columnService.findLower(col);
		res.setSucceed(sCols,Const.DATA_SUCCEED);
		return res;
	}
	
	@RequestMapping("/col/info")
	@ResponseBody
	public AjaxRes findByPage(Column col, Page<Column> pages){
		AjaxRes res = new AjaxRes();
		pages = columnService.findByPage(col, pages);
		res.setSucceed(pages, Const.DATA_SUCCEED);
		return res;
	}
	
	@RequestMapping("/col/detail")
	@ResponseBody
	public AjaxRes find(Column col){
		AjaxRes res = new AjaxRes();
		col = columnService.find(col).get(0);
		res.setSucceed(col, Const.DATA_SUCCEED);
		return res;
	}
	
	@RequestMapping("/col/add")
	@ResponseBody
	public AjaxRes addCol(@RequestBody Map<String,Object> map){
		AjaxRes res = new AjaxRes();
		//Map 转为JSON 再反序列化为bean
		String json = JSONArray.toJSONString(map.get("obj"));
		Column col = JSON.parseArray(json, Column.class).get(0);
		//添加默认封面
		if(col.getCover() == null || col.getCover() == ""){
			col.setCover("SpringDemo/upload/file/bbc.jpg");
		}
		columnService.insert(col);
		res.setSucceedMsg("添加成功");
		return res;
	}

	@RequestMapping("/col/upd")
	@ResponseBody
	public AjaxRes updateCol(@RequestBody Map<String,Object> map){
		AjaxRes res = new AjaxRes();
		String json = JSONArray.toJSONString(map.get("obj"));
		Column col = JSON.parseArray(json, Column.class).get(0);
		//添加默认封面
		if(col.getCover() == null || col.getCover() == ""){
			col.setCover("SpringDemo/upload/file/bbc.jpg");
		}
		columnService.update(col);
		res.setSucceedMsg("信息更新成功");
		return res;
	}
	
	@RequestMapping("/col/del")
	@ResponseBody
	public AjaxRes delete(Column col){
		AjaxRes res = new AjaxRes();
		columnService.delete(col);
		res.setSucceed("删除成功");
		return res;
	}
	
	@RequestMapping("/col/delall")
	@ResponseBody
	public AjaxRes delAllCols(@RequestBody List<Column> cols){
		AjaxRes res = new AjaxRes();
		columnService.deleteBatch(cols);
		res.setSucceedMsg("删除成功");
		return res;
	}
	
	@RequestMapping("/col/del/pic")
	@ResponseBody
	public AjaxRes delPis(Column cols){
		AjaxRes res = new AjaxRes();
		cols = columnService.find(cols).get(0);
		cols.setCover("");
		columnService.update(cols);
		res.setSucceedMsg("删除成功");
		return res;
	}
	
}
